# -*- coding:utf-8 -*-
'''pygame练习第一部分，为了更好的理解整个游戏设计的代码，
分步来详细学习这个游戏代码，理解类的定义及实例化，应用'''

import pygame
from pygame.locals import *
import time
import random


class HeroPlane:
    '''玩家飞机类'''

    def __init__(self, screen_temp):
        self.x = 200
        self.y = 400
        self.screen = screen_temp
        self.image = pygame.image.load("./images/me.png")
        self.bullet_list = []

    def display(self):
        # 绘制子弹,判断子弹是否越界，如果越界b.move()为True移除子弹对象
        for b in self.bullet_list:
            b.display()
            if b.move():
                self.bullet_list.remove(b)

        '''绘制飞机'''
        self.screen.blit(self.image, (self.x, self.y))

    def move_left(self):
        '''向左移动飞机'''
        self.x -= 5
        if self.x <= 0:
            self.x = 0

    def move_right(self):
        '''向左移动飞机'''
        self.x += 5
        if self.x >= 406:
            self.x = 406

    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))


class Bullet:
    '''子弹类'''

    def __init__(self, screen_temp, x, y):
        self.x = x + 53
        self.y = y
        self.screen = screen_temp
        self.image = pygame.image.load("./images/pd.png")

    def display(self):
        '''绘制子弹'''
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5
        if self.y <= -20:
            return True


class EnemyBullet(Bullet):  # 定义敌机子弹，继承Bullet类

    def __init__(self, screen_temp, x, y):
        Bullet.__init__(self, screen_temp, x, y)
        self.x = x + 50  # self.x,self.y设置子弹初始位置
        self.y = y + 80
        self.image = pygame.image.load("./images/enemy_bullet.png")

    def move(self):  # 子弹飞行速度，判断子弹出界，移除子弹
        self.y += 10
        if self.y >= 568:
            return True


class EnemyPlane:
    '''
    敌机类
    '''
    def __init__(self, screen_temp):
        self.x = random.choice(range(408))
        self.y = -75
        self.screen = screen_temp
        self.image = pygame.image.load("./images/e1.png")
        self.ebullet_list = []  # 敌机子弹列表

    def display(self):

        # 绘制敌机
        self.screen.blit(self.image, (self.x, self.y))

    def move(self, hero,screen_temp):
        '''敌机移动'''
        self.y += 4
        # 如果敌机飞出屏幕，返回True
        if self.y > 568:
            return True
        # 敌机和子弹的碰撞检测(矩阵检测)
        for bo in hero.bullet_list:  # 遍历hero子弹列表
            # 进行矩阵型碰撞检测
            if bo.x > self.x + 12 and bo.x < self.x + 92 and bo.y > self.y + 10 and bo.y < self.y + 60:
                hero.bullet_list.remove(bo) # 敌机和碰撞后，移除子弹
                bx = bo.x -30
                by = bo.y -30
                boom(bx,by,screen_temp) #敌机中弹，显示爆炸效果
                return True

    def enemy_fire(self,hero_temp):  # 敌机随机发射子弹
        if random.choice(range(30)) == 5:
            ebullet = EnemyBullet(self.screen, self.x, self.y)
            self.ebullet_list.append(ebullet)

        for eb in self.ebullet_list:
            eb.display()  # 绘制敌机子弹
            if eb.move():  # 如果敌机子弹越界，移除子弹
                self.ebullet_list.remove(eb)
            # 检测敌机子弹和Hero碰撞
            if eb.x > hero_temp.x + 10 and eb.x < hero_temp.x + 85 and eb.y > hero_temp.y  and eb.y < hero_temp.y + 10:
                self.ebullet_list.remove(eb)  # 敌机子弹和Hero碰撞后，移除子弹
                return True

def boom(x,y,screen):#敌机和子弹碰撞时显示爆炸效果
    i = 1
    for i in range(1,5):
        boomi = pygame.image.load("./images/boom"+str(i)+".png")
        screen.blit(boomi, (x, y))
        pygame.display.update()




def key_control(hero_temp):
    """
    键盘控制函数
    获取按键信息，并识别
    执行对应的操作
    :return:
    """
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[K_LEFT]:
        hero_temp.move_left()
    elif pressed_keys[K_RIGHT]:
        hero_temp.move_right()

    if pressed_keys[K_SPACE]:
        hero_temp.fire()


def main():
    # 设置游戏窗口
    screen = pygame.display.set_mode((512, 568), 0, 0)
    # 游戏背景
    background = pygame.image.load("./images/bg2.jpg")
    #gameover图标
    gameover = pygame.image.load("./images/gameover.jpg")
    #continue 图标
    continuegame = pygame.image.load("./images/continue.png")
    # 创建玩家飞机
    hero = HeroPlane(screen)  # 传参数？？？？什么    时候需要传参数，什么时候不用传

    m = -968
    enemylist = []  # 存放敌机的列表
    while True:  # 画面循环，维持画面;设置背景图片自下向上移动，达到飞机向上飞行的效果
        screen.blit(background, (0, m))
        m += 2
        if m >= -200:
            m = -968  # 当背景图片自下而上，y轴坐标到达-200时，图片坐标跳回，循环图片
        # 定时显示，节省内存
        time.sleep(0.04)
        hero.display()

        # 控制飞机飞行
        key_control(hero)

        if random.choice(range(50)) == 25:  # 随机产生敌机
            enemylist.append(EnemyPlane(screen))
        # 遍历敌机列表，绘制敌机
        for em in enemylist:
            em.display()  # 绘制敌机
            if em.move(hero,screen):  # 子弹和敌机的碰撞检测,碰撞后移除敌机
                enemylist.remove(em)
            # 敌机子弹或敌机和Hero的碰撞检测,碰撞后显示game over
            if em.enemy_fire(hero) or (hero.y<em.y+60 and hero.y > em.y- 70 and hero.x>em.x-80 and hero.x<em.x+80):
                print("GameOver")

                while True:#显示game over和按空格继续
                    screen.blit(gameover, (56, 178))
                    screen.blit(continuegame,(185,500))
                    pygame.display.update()
                    time.sleep(0.04)
                    '''event 和pressed_keys要联合用才能按空格继续，单独用pressed_keys不行？？？'''
                    for event in pygame.event.get():
                        if event.type == QUIT:
                            exit()
                    #按空格键可以继续游戏
                    pressed_keys = pygame.key.get_pressed()
                    if pressed_keys[K_SPACE]:
                        break

                '''游戏重新开始'''
                screen = pygame.display.set_mode((512, 568), 0, 0)
                # 游戏背景
                background = pygame.image.load("./images/bg2.jpg")
                # gameover图标
                gameover = pygame.image.load("./images/gameover.jpg")
                # continue 图标
                continuegame = pygame.image.load("./images/continue.png")
                # 创建玩家飞机
                hero = HeroPlane(screen)  # 传参数？？？？什么    时候需要传参数，什么时候不用传

                m = -968
                enemylist = []

        pygame.display.update()


if __name__ == '__main__':
    main()
